/********************************************************************************
 * Project Name		[JavaEE_Web]
 * File Name     	[JdbcUtils.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package mvc.utils;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

/**
 * JDBC工具类
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public final class JdbcUtils {

	private static DataSource pool;
	private static JdbcUtils instance = new JdbcUtils();

	static {
		try {
			pool = new ComboPooledDataSource();
		} catch (Exception e) {
			throw new ExceptionInInitializerError(e);
		}
	}

	private JdbcUtils() {
		super();
	}

	public static JdbcUtils getInstance() {
		return instance;
	}

	/**
	 * 获取连接
	 * 
	 * @return
	 * @throws SQLException
	 */
	public Connection getConnection() throws SQLException {
		return pool.getConnection();
	}

	/**
	 * 关闭连接
	 * 
	 * @param connection
	 * @throws SQLException
	 */
	public void releaseConnection(Connection connection) throws SQLException {
		if (connection != null) {
			connection.close();
		}
	}
}
